package com.pay.paytoken.activity;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.pay.paytoken.R;
import com.pay.paytoken.base.BaseTitleActivity;

import appUtils.PayTokenException;

/**
 * Created by lj on 2017/4/26 0026.
 */

public class HelpDetailActivity extends BaseTitleActivity {

    private Intent mIntent;
    private WebView mWebView;
    private ProgressBar mPbProgress;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            try {
                mWebView.clearHistory();
                mWebView.stopLoading();
                mWebView.destroy();
            } catch (Exception e) {
                e.printStackTrace();
                PayTokenException.getInstance().saveExceptionFile("HelpDetailActivity mWebView error:"
                        + e.getMessage());
            }
        }

    }

    @Override
    public int setView() {
        return R.layout.activity_help_webview;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mIntent = getIntent();
    }

    @Override
    public void initView() {
        mWebView = (WebView) findViewById(R.id.wv_webView);
        mPbProgress = (ProgressBar) findViewById(R.id.pb_progress);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setSupportZoom(true);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setDefaultFontSize(18);
        mWebView.setInitialScale(100);
        //Screen adaptive
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        //WebView loading web resources
        mTitleLeft.setVisibility(View.VISIBLE);
        mTitleRight.setVisibility(View.INVISIBLE);
        mTitleName.setText(mIntent.getStringExtra("title"));
        mWebView.loadUrl(mIntent.getStringExtra("URL"));
        // cover WebView Default to a third party or the system default browser to icon_password_open the web page，webpage use WebView Open
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //The return value is true when the control to the Web View to icon_password_open, to false call system browser browser or a third party
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {//监听网页加载
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if (newProgress == 100) {
                    mPbProgress.setVisibility(View.GONE);
                } else {

                    mPbProgress.setVisibility(View.VISIBLE);
                    mPbProgress.setProgress(newProgress);
                }

                super.onProgressChanged(view, newProgress);

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebView.canGoBack()) {//Webview is not in the first page，Return on a page
                mWebView.goBack();
                return true;
            } else {
                // webview in the first page, exit the program directly
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
